package com.secure.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

public class PayUtil {
	
	public static String sign(String txt, String charSet) {
		String sign = "";
		if (StringUtils.isBlank(txt))
			return sign;
		charSet = (null == charSet ? "utf-8" : charSet);
		try {
			String md51 = DigestUtils.md5Hex(txt.getBytes(charSet));
			sign = DigestUtils.md5Hex(md51.getBytes(charSet));
		} catch (Exception e) {
			throw new RuntimeException("PaySign MD5 has exception");
		}
		return sign;
	}

	// 两数相乘
	public static double mulToDouble(String v1, String v2) {
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.multiply(b2).doubleValue();
	}

	public static int mulToInt(String v1, String v2) {
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.multiply(b2).intValue();
	}

	// 两数相除
	public static double divideToDouble(String v1, String v2) {
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.divide(b2, 2, BigDecimal.ROUND_DOWN).doubleValue();
	}

	// 两数相加
	public static double addToDouble(String v1, String v2) {
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.add(b2).doubleValue();
	}

	// 两数相减
	public static double subtractToDouble(String v1, String v2) {
		BigDecimal b1 = new BigDecimal(v1);
		BigDecimal b2 = new BigDecimal(v2);
		return b1.subtract(b2).doubleValue();
	}

	// 生成表单
	public static String buildForm(String Url, Map<String, String> m, String frmMethod) {
		if (StringUtils.isEmpty(frmMethod))
			frmMethod = "POST";
		StringBuffer sbHtml = new StringBuffer();
		sbHtml.append("<form name=\"buildFrm\" action=\"" + Url + "\" method=\"" + frmMethod + "\">");

		if (m != null && m.size() > 0) {
			List<String> keys = new ArrayList<String>(m.keySet());
			for (int i = 0; i < keys.size(); i++) {
				String name = (String) keys.get(i);
				String value = (String) m.get(name);
				sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
			}
		}
		sbHtml.append("</form><script>document.forms['buildFrm'].submit();</script>");
		return sbHtml.toString();
	}

	// 格式化时间
	public static String formatTime(Date dtime, String formatStr) {
		if (dtime == null)
			dtime = new Date();
		if (StringUtils.isEmpty(formatStr))
			formatStr = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat outFormat = new SimpleDateFormat(formatStr);
		return outFormat.format(dtime);
	}

	// 格式化数字
	public static String formatNumber(Double amount, String numFormat) {
		if(StringUtils.isEmpty(numFormat))
			numFormat = "0.00";
		DecimalFormat df = new DecimalFormat(numFormat);
		return df.format(amount);
	}

	// 获取编码字符集
	public static String getCharacterEncoding(HttpServletRequest request, HttpServletResponse response) {
		if (null == request || null == response) {
			return "utf-8";
		}
		String enc = request.getCharacterEncoding();
		if (null == enc || "".equals(enc)) {
			enc = response.getCharacterEncoding();
		}
		if (StringUtils.isEmpty(enc))
			enc = "utf-8";
		return enc;
	}

	// 将request 中的 请求参数 重新拼装成Map<String,String>
	public static Map<String, String> requestParams2Map(HttpServletRequest req) {
		Map<String, String> params = new HashMap<String, String>();
		Map<?, ?> requestParams = req.getParameterMap();
		for (Iterator<?> iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			params.put(name, valueStr);
		}
		return params;
	}

}
